package com.xtj.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQUtils {
    private static ConnectionFactory connectionFactory;
    static {
        //创建链接mq的连接工厂对象
        connectionFactory = new ConnectionFactory();
        //设置连接rabbitmq主机
        connectionFactory.setHost("124.222.224.71");
        //设置端口号
        connectionFactory.setPort(5672);
        //设置连接那个虚拟主机
        connectionFactory.setVirtualHost("/test");
        //设置访问虚拟机的用户名和密码
        connectionFactory.setUsername("test");
        connectionFactory.setPassword("123456");
    }
    //定义提供连接对象的方法
    public static Connection getConnection(){
        try {
            //返回数据
            return connectionFactory.newConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static void closeConnectionAndChanel(Channel channel,Connection connection){
     try {
         if(channel!=null) channel.close();
         if(connection!=null) connection.close();
     }catch (Exception e){
         e.printStackTrace();
     }

    }
}
